/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m003.bl;

import com.vaadin.data.util.BeanItemContainer;
import ep.m003.ui.controllers.M003Controller;
import ep.persistence.controllers.T005T007JpaController;
import ep.persistence.controllers.T007IllnessJpaController;
import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.SavePatientIllnessBean;
import ep.persistence.entities.T005Patient;
import ep.persistence.entities.T005T007;
import ep.persistence.entities.T007Illness;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author User
 */
@Path("bl003")
public class BL003Controller {

    @POST
    @Path("/sillness")
    @Produces(MediaType.TEXT_PLAIN)
    public String savePatientIllness(SavePatientIllnessBean bean) {
        try {
            T005Patient patient = bean.getPatient();
            Collection t005t007Collection = bean.getT005t007Collection();
            patient.setT005T007Collection(t005t007Collection);
            T005T007JpaController jpaController = new T005T007JpaController();
            for (Iterator it = t005t007Collection.iterator(); it.hasNext();) {
                T005T007 t005t007 = (T005T007) it.next();
                t005t007.setT005Patient(patient);
                if (t005t007.getId() == null) {
                    jpaController.create(t005t007);
                } else {
                    jpaController.edit(t005t007);
                }
            }
            return "0";
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(M003Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(M003Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(M003Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "-1";
    }

    @GET
    @Path("/illnesses")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T007Illness> getIllnessCollection() {
        T007IllnessJpaController jpaController = new T007IllnessJpaController();
        Collection collection = jpaController.findT007IllnessEntities();
        if (collection.isEmpty()) {
            return null;
        } else {
            return jpaController.findT007IllnessEntities();
        }
    }
}
